package com.sugon.modules.mold.service.impl;

import cn.hutool.core.util.StrUtil;
import com.sugon.dao.ModuleReplacePartDao;
import com.sugon.entity.ModuleMaintainPlanEntity;
import com.sugon.modules.base.consts.SysApproveStatusConst;
import com.sugon.modules.base.model.dto.HandleApproveResult;
import com.sugon.modules.base.service.impl.AbstractErmsBaseApproveManager;
import com.sugon.modules.mold.consts.MoldReplacePartProgress;
import com.sugon.modules.mold.service.IMoldMaintenanceTicketManager;
import com.sugon.modules.sys.consts.ProcessProgressStatus;
import com.sugon.service.ModuleMaintainPlanService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ： YuXD
 * @description： 模具维护业务Manager实现类
 * @date ： 2020-08-13 上午 10:50
 * @version: 1.0
 */
@Service
public class MoldMaintenanceTicketManagerImpl extends AbstractErmsBaseApproveManager implements IMoldMaintenanceTicketManager {

    @Autowired
    private ModuleMaintainPlanService moduleMaintainPlanService;
    @Autowired
    private SysProcessInfoService processInfoService;
    @Autowired
    private SysMsgService msgService;
    @Autowired
    private ModuleReplacePartDao moldReplacePartDao;

    /**
     * commitApprove
     *
     * @param moduleMaintainPlan 模具维护工单
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean commitApprove(ModuleMaintainPlanEntity moduleMaintainPlan) {
        moduleMaintainPlan.setReplacePartProgress(MoldReplacePartProgress.DEPT_APPROVING);
        boolean flag = moduleMaintainPlanService.update(moduleMaintainPlan) > 0;
        if (flag) {
            // 更新换件记录状态
            moldReplacePartDao.updateProgress(moduleMaintainPlan.getPlanName(), MoldReplacePartProgress.DEPT_APPROVING);
            String moldMaintenanceTicketId = moduleMaintainPlan.getId();
            // 保存流程信息
            processInfoService.saveProcessInfo(moldMaintenanceTicketId, ProcessInfoEnum.MOLD_MAINTENANCE_RP, 0, null);
            // 添加备件审批事项
            msgService.addMoldRepairApproveItem(moldMaintenanceTicketId, 0);
        }
        return flag;
    }

    /**
     * handleWorkshopApprove
     *
     * @param approveItemId 审批事项ID
     * @param approveStatus 审批状态
     * @param remark        审批备注
     * @return
     */
    @Override
    public HandleApproveResult handleWorkshopApprove(String approveItemId, Integer approveStatus, String remark) {
        HandleApproveResult handleApproveResult = new HandleApproveResult();
        ModuleMaintainPlanEntity moldMaintenanceTicket = moduleMaintainPlanService.queryObject(approveItemId);
        org.springframework.util.Assert.notNull(moldMaintenanceTicket, "模具维护工单不存在，请确认！");
        // 校验规程进度
        org.springframework.util.Assert.isTrue(MoldReplacePartProgress.DEPT_APPROVING.equals(moldMaintenanceTicket.getReplacePartProgress()), "只可以审核[部门审核]的模具维护工单！");
        handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
        handleApproveResult.setTitle(StrUtil.format("模具维护工单[{}]换件审核", moldMaintenanceTicket.getPlanName()));
        // 如果审核通过，则更新进度
        Integer moldReplacePartProgress;
        if (approveStatus == SysApproveStatusConst.PASSED) {
            moldReplacePartProgress = MoldReplacePartProgress.PASSED;
            handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.END);
            // 更新所有换件记录状态
            moldReplacePartDao.updateProgress(moldMaintenanceTicket.getPlanName(), MoldReplacePartProgress.PASSED);
        } else {
            moldReplacePartProgress = MoldReplacePartProgress.NOT_PASSED;
            // 未通过审批
            handleApproveResult.setProcessProgressStatus(ProcessProgressStatus.ING);
        }
        moldMaintenanceTicket.setReplacePartProgress(moldReplacePartProgress);
        int rows = moduleMaintainPlanService.update(moldMaintenanceTicket);
        if (rows > 0) {
            handleApproveResult.setUserId(moldMaintenanceTicket.getRepairUser());
            // 更新所有换件记录状态
            moldReplacePartDao.updateProgress(moldMaintenanceTicket.getPlanName(), moldReplacePartProgress);
        }
        return handleApproveResult;
    }

}
